package com.fanshilz.flower2025.apps.admin.wechatUser.controller;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.fanshilz.flower2025.apps.admin.wechatUser.req.IdentityUpdateReq;
import com.fanshilz.flower2025.apps.admin.wechatUser.req.WechatUserIdentityTableReq;
import com.fanshilz.flower2025.apps.admin.wechatUser.req.WechatUserTableReq;
import com.fanshilz.flower2025.apps.admin.wechatUser.resp.WechatUserIdentityTableDetailResp;
import com.fanshilz.flower2025.apps.admin.wechatUser.resp.WechatUserTableDetailResp;
import com.fanshilz.flower2025.business.flower.userIdentity.entity.UserIdentity;
import com.fanshilz.flower2025.business.flower.userIdentity.entity.UserIdentityStatusEnum;
import com.fanshilz.flower2025.business.flower.userIdentity.service.IUserIdentityService;
import com.fanshilz.flower2025.business.flower.wechatUser.entity.UserCheckStatusEnum;
import com.fanshilz.flower2025.business.flower.wechatUser.entity.WechatUser;
import com.fanshilz.flower2025.business.flower.wechatUser.service.IWechatUserService;
import com.fanshilz.flower2025.framework.aliyun.sms.AliyunSmsUtil;
import com.fanshilz.flower2025.framework.resp.Resp;
import com.fanshilz.flower2025.framework.resp.TableResp;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import io.github.linpeilie.Converter;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.fanshilz.flower2025.business.flower.userIdentity.entity.table.UserIdentityTableDef.USER_IDENTITY;
import static com.fanshilz.flower2025.business.flower.wechatUser.entity.table.WechatUserTableDef.WECHAT_USER;

@RequestMapping("/admin/wechatUser")
@RestController
public class AdminWechatUserController {

    @Resource
    private IWechatUserService wechatUserService;
    @Resource
    private IUserIdentityService userIdentityService;
    @Resource
    private Converter converter;

    @PostMapping("/identity/update")
    public Resp<Void> identityUpdateReq(@RequestBody @Valid IdentityUpdateReq req) {
        UserIdentity ui = converter.convert(req, UserIdentity.class);

        WechatUser one = wechatUserService.getOne(QueryWrapper.create()
                .select(WECHAT_USER.USER_ID)
                .where(WECHAT_USER.USER_ID.eq(req.getCreateUserId())));

        if (UserIdentityStatusEnum.PASS.getValue().equals(req.getIdentityStatusValue())) {
            // 审核通过
            if (userIdentityService.count(QueryWrapper.create()
                    .where(USER_IDENTITY.CREATE_USER_ID.eq(req.getCreateUserId()))
                    .where(USER_IDENTITY.IDENTITY_STATUS_VALUE.eq(UserIdentityStatusEnum.PASS.getValue()))) == 0) {
                // 该用户当前认证是第一个认证身份
                ui.setIdentitySwitch(true);

                WechatUser wu = wechatUserService.getById(req.getCreateUserId());
                if (UserCheckStatusEnum.FALSE.equals(wu.getUserCheckStatus())) {
                    // 用户已经认证
                    wechatUserService.updateById(WechatUser.builder()
                            .userId(wu.getUserId())
                            .userCheckStatus(UserCheckStatusEnum.TRUE)
                            .build());
                }
            }

            if (ObjUtil.isNotNull(one) && StrUtil.isNotBlank(one.getUserMobile())) {
                AliyunSmsUtil.sendTemplateCode("SMS_479025666", one.getUserMobile());
            }
        } else if (UserIdentityStatusEnum.REJECT.getValue().equals(req.getIdentityStatusValue())) {
            if (ObjUtil.isNotNull(one) && StrUtil.isNotBlank(one.getUserMobile())) {
                AliyunSmsUtil.sendTemplateCode("SMS_479145666", one.getUserMobile());
            }
        }

        userIdentityService.updateById(ui);

        return Resp.success();
    }

    @PostMapping("/identity/table")
    public Resp<TableResp<WechatUserIdentityTableDetailResp>> identityTable(@RequestBody @Valid WechatUserIdentityTableReq req) {
        Page<WechatUserIdentityTableDetailResp> page = userIdentityService.getMapper()
                .paginateWithRelationsAs(Page.of(req.getCurrent(), req.getPageSize(), req.getTotal()), QueryWrapper.create(), WechatUserIdentityTableDetailResp.class);
        return Resp.success(new TableResp<>(page.getRecords(), page.getTotalRow()));
    }

    @PostMapping("/table")
    public Resp<TableResp<WechatUserTableDetailResp>> table(@RequestBody @Valid WechatUserTableReq req) {
        Page<WechatUserTableDetailResp> page = wechatUserService.pageAs(Page.of(req.getCurrent(), req.getPageSize(), req.getTotal()),
                QueryWrapper.create(), WechatUserTableDetailResp.class);
        return Resp.success(new TableResp<>(page.getRecords(), page.getTotalRow()));
    }

}
